<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<link rel="stylesheet" href="<c:url value='/resource/js/app/jquery-validation/1.11.0/jquery.validate.css'/>">
<script src="<c:url value='/resource/js/app/jquery-validation/1.11.0/jquery.validate.js'/>"></script>
<script src="<c:url value='/resource/js/rsa/Barrett.js'/>"></script>
<script src="<c:url value='/resource/js/rsa/BigInt.js'/>"></script>
<script src="<c:url value='/resource/js/rsa/RSA.js'/>"></script>
<div class="ucr-centent user-info marginTop60">
    <h3 class="ucr-title">账号注销</h3>
    <form class="form-horizontal" role="form" id="logoff_form">
        <div class="form-group">
            <label class="col-sm-2 control-label">手机号：</label>
            <div class="col-sm-6">
                <input type="text" id="acountId" class="form-control" value="${current_member.acountId}" readonly="readonly">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label"><span class="text-red">*</span>身份证号：</label>
            <div class="col-sm-6">
                <input type="text" id="idNum" name="idNum" class="form-control" value="${current_member.idNum}">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label"><span class="text-red">*</span>密码：</label>
            <div class="col-sm-6">
                <input type="password" id="password" name="password" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label"><span class="text-red">*</span>验证码：</label>
            <div class="col-sm-4">
                <input type="text" id="code" name="code" placeholder="请输入验证码" class="form-control">
            </div>
            <div class="col-sm-2">
                <button class="btn" type="button" id="get_code">获取验证码</button>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-8">
                <button type="button" class="btn btn-lg btn-blue" onclick="doLogOff()">保存</button>
            </div>
        </div>
    </form>
</div>
<script>
    $(function(){
        $("#get_code").click(function () {
            var phone=$("#acountId").val();
            if(phone==''||phone==null){
                toast("手机号不能为空");
                return;
            }
            $.ajax({
                type:'get',
                url:"<c:url value='/members/sms_code'/>",
                data:{
                    phone:phone,
                    source:"logoff"
                },
                success:function(res){
                    if(res.succ){
                        toast("已发送验证码");
                    }else{
                        toast(res.msg);
                    }
                },
                error:function (res) {
                    toast("服务器异常");
                }
            });
        });
        init();
    });
    function init(){
        $("#logoff_form").validate({
            rules:{
                idNum:"required",
                password:"required",
                code:"required"
            },
            messages:{
                idNum:"请输入身份证号码",
                password:"请输入密码",
                code:"请输入验证码"
            },
            errorPlacement: function(error, element) {  //验证不通过处理
                toast(error[0].innerText);
            }
        });
    }
    function doLogOff(){
        if($("#logoff_form").valid()){
            $.ajax({
                type:'post',
                url:"<c:url value='/members/logoff'/>",
                data:JSON.stringify({
                    acountId:$("#acountId").val(),
                    code:$("#code").val(),
                    idNum:$("#idNum").val(),
                    password:getEncrypted($("#password").val()),
                    source:"logoff"
                }),
                contentType: 'application/json;charset=utf-8',
                dataType:"json",
                success:function(res){
                    if(res.succ){
                        toast("账号注销成功");
                        window.location.href="<c:url value='/'/>";
                    }else{
                        toast(res.msg);
                    }
                },
                error:function (res) {
                    toast("服务器异常");
                }
            });
        }
    }
    function getEncrypted(data) {
        console.info(data);
        setMaxDigits(130); //1024位就是130，2048位就是260.。。。。。。
        var key1 = new RSAKeyPair("${e}","","${n}");	//从服务端获取到的n和e可以得出公钥
        var encode_content = encodeURIComponent(data);
        var encryptData = encryptedString(key1,encode_content);
        return encryptData;
    }
</script>